﻿using Aspose.Cells.Charts;
using HDSmartCommunity.BUS;
using HDSmartCommunity.BUS.MqttBUS;
using HDSmartCommunity.Models;
using HDSmartCommunity.Serial;
using Microsoft.VisualStudio.Web.CodeGeneration.Contracts.Messaging;
using Newtonsoft.Json;
using Org.BouncyCastle.Asn1.X500;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Timer = System.Timers.Timer;


namespace HDSmartCommunity.DevicePlatform
{
    public class SHGDDeviceSerialData
    {
        public SerialPort myS0SerialPort_1 = new SerialPort();
        public SerialPort myS1SerialPort_2 = new SerialPort();
        public LogPrint logPrint;
        public string topic;
        public string topic_2;
        public MqttClientBUS clientBUS;
        public List<MqttDeviceInfoDataPoint> last_point;
        public static CancellationTokenSource cmd_tokenSource = new CancellationTokenSource();
        public CancellationToken cmd_token = cmd_tokenSource.Token;
        public ManualResetEvent cmd_resetEvent = new ManualResetEvent(true);
        public DateTime lastreceive = DateTime.Now;
        public DateTime dt_lastreceive = DateTime.Now;
        public bool isconnect = false;
        public string device_id;
        public string number;
        public Socket server;
        public Thread thread;
        public string areaid;
        public int liftid;
        public bool ft_status = true;
        public bool dt_status = true;
        public string ft_point;
        public string dt_point;
        public DateTime ft_date;
        public DateTime dt_date;
        public DateTime mqtt_date;
        public string server_ip;
        public int server_port;
        public int mqttsend;
        public int DataSimulation;
        public int VirtualInterval;
        public List<MqttDeviceInfoDataPoint> dataSimulation_point = new List<MqttDeviceInfoDataPoint>();
        public int ele_type;
        public DTParamInfo dTParamInfo = new DTParamInfo();
        public FTParamInfo fTParamInfo = new FTParamInfo();
        public int mqttlog = 0;

        public List<int> phy_list = new List<int>();//物理楼层列表
        public List<string> show_list = new List<string>();//显示楼层列表
        public List<int> act_list = new List<int>();//实际楼层列表


        public static CancellationTokenSource tokenSource = new CancellationTokenSource();
        public CancellationToken token = tokenSource.Token;
        public ManualResetEvent resetEvent = new ManualResetEvent(true);

        public static CancellationTokenSource dt_tokenSource = new CancellationTokenSource();
        public CancellationToken dt_token = dt_tokenSource.Token;
        public ManualResetEvent dt_resetEvent = new ManualResetEvent(true);

        public int off_time = 0;


        #region ttys0 电梯
        public void OpenS0SerialPort(string portName, int baudRate, int parity, int dataBits, int stopBits)
        {
            try
            {
                SetS0SerialPortConfig(portName, baudRate, parity, dataBits, stopBits);
                myS0SerialPort_1.Open();
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
                logPrint.Log("ttyS0打开异常" + e.ToString());
            }
        }

        private Timer SerialPortRecvTimerS0_1;
        private static int byteLength = 40960;
        public byte[] byteSerialPortRecvS0_1 = new byte[byteLength];
        public event EventHandler<SerialPort485_1RecvEventArgs> ReceivedDataEventS0_1;
        private static int pSerialPortRecv_S0_1 = 0;

        public class SerialPort485_1RecvEventArgs : EventArgs
        {
            /// <summary>
            /// 广播 收到的串口数据
            /// </summary>
            public readonly byte[] RecvData_485_1 = new byte[byteLength];

            /// <summary>
            /// 收到数据 的 长度
            /// </summary>
            public readonly int RecvDataLength_485_1;

            /// <summary>
            /// 将 收到的数据 转化成 待广播的数据
            /// </summary>
            public SerialPort485_1RecvEventArgs(byte[] recvData, int recvDataLength)
            {
                recvData.CopyTo(RecvData_485_1, 0);
                RecvDataLength_485_1 = recvDataLength;
            }
        }

        private void SetS0SerialPortConfig(string portName, int baudRate, int parity, int dataBits, int stopBits)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                portName = $"/dev/{portName}";
            }
            // 串口 参数设置
            myS0SerialPort_1.PortName = portName;
            myS0SerialPort_1.BaudRate = baudRate;
            switch (parity)
            {
                case 0:
                default:
                    myS0SerialPort_1.Parity = Parity.None;
                    break;
                case 1:
                    myS0SerialPort_1.Parity = Parity.Odd;
                    break;
                case 2:
                    myS0SerialPort_1.Parity = Parity.Even;
                    break;
                case 3:
                    myS0SerialPort_1.Parity = Parity.Mark;
                    break;
                case 4:
                    myS0SerialPort_1.Parity = Parity.Space;
                    break;
            }
            myS0SerialPort_1.DataBits = ((4 < dataBits) && (dataBits < 9)) ? dataBits : 8;
            switch (stopBits)
            {
                case 0:
                    myS0SerialPort_1.StopBits = StopBits.None;
                    break;
                case 1:
                default:
                    myS0SerialPort_1.StopBits = StopBits.One;
                    break;
                case 2:
                    myS0SerialPort_1.StopBits = StopBits.OnePointFive;
                    break;
                case 3:
                    myS0SerialPort_1.StopBits = StopBits.Two;
                    break;
            }
            //my485SerialPort_1.ReadTimeout = 200;
            myS0SerialPort_1.RtsEnable = true;
            myS0SerialPort_1.DataReceived += new SerialDataReceivedEventHandler(DataReceived_ttyS0);

            // 串口 接收数据处理定时器 参数设置
            SerialPortRecvTimerS0_1 = new System.Timers.Timer();
            SerialPortRecvTimerS0_1.Interval = 100;
            SerialPortRecvTimerS0_1.AutoReset = false;
            SerialPortRecvTimerS0_1.Elapsed += new ElapsedEventHandler(SPRecvTimer_Tick_ttyS0_1);
        }

        private void SPRecvTimer_Tick_ttyS0_1(object sender, EventArgs e)
        {
            byte[] TemporaryData = new byte[byteLength];
            int TemporaryDataLength = 0;

            if (ReceivedDataEventS0_1 != null)
            {
                byteSerialPortRecvS0_1.CopyTo(TemporaryData, 0);
                TemporaryDataLength = pSerialPortRecv_S0_1;

                ReceivedDataEventS0_1.Invoke(this, new SerialPort485_1RecvEventArgs(TemporaryData, TemporaryDataLength));
                // 数据处理完后，将指针指向数据头，等待接收新的数据
                pSerialPortRecv_S0_1 = 0;
            }
        }

        private void DataReceived_ttyS0(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPortRecvTimerS0_1.Stop();
            try
            {
                byte[] ReadBuf = new byte[myS0SerialPort_1.BytesToRead];
                if (pSerialPortRecv_S0_1 > byteLength - ReadBuf.Length)
                {
                    pSerialPortRecv_S0_1 = 0;
                }
                myS0SerialPort_1.Read(ReadBuf, 0, ReadBuf.Length);
                ReadBuf.CopyTo(byteSerialPortRecvS0_1, pSerialPortRecv_S0_1);
                pSerialPortRecv_S0_1 += ReadBuf.Length;
                if (mqttlog == 1)
                {
                    SerialDataSaveLogHelper.Log(ToHexs(ReadBuf));
                }
                for (int i = 0; i < byteSerialPortRecvS0_1.Length; i++)
                {
                    if (byteSerialPortRecvS0_1[i] == 0x1B && byteSerialPortRecvS0_1[i + 1] == 0x5B && byteSerialPortRecvS0_1[i + 2] == 0x48)
                    {
                        byte[] buffer = new byte[34];
                        for (int j = 0; j < 34; j++)
                        {
                            buffer[j] = byteSerialPortRecvS0_1[i + 3 + j];
                        }
                        Array.Clear(byteSerialPortRecvS0_1, i, buffer.Length + 3);
                        LogHelper.Log(ToHexs(buffer));
                        int run = 0;
                        int pks = 0;
                        int ins = 0;
                        int falut = 0;
                        int heart = 1;
                        byte[] data = new byte[3];
                        for (int k = 0; k < 3; k++)
                        {
                            data[k] = buffer[k + 5];
                        }

                        List<MqttDeviceInfoDataPoint> list = new List<MqttDeviceInfoDataPoint>();
                        MqttDeviceInfoDataPoint dataPoint = new MqttDeviceInfoDataPoint();
                        string ele_status = HexStringToASCII(data);
                        if (mqttlog == 1)
                        {
                            LogHelper.Log("电梯状态" + ele_status);
                        }
                        bool send_mqtt = false;
                        string data_str = ToHexs(buffer);

                        if (data_str != dt_point)
                        {
                            if (mqttsend == 1)
                            {
                                send_mqtt = true;
                            }
                            dt_point = data_str;
                        }
                        else
                        {
                            if (mqttsend == 1)
                            {
                                TimeSpan mqtt_secondSpan = new TimeSpan(DateTime.Now.Ticks - mqtt_date.Ticks);
                                if (mqtt_secondSpan.TotalMilliseconds > 1000)
                                {
                                    send_mqtt = true;
                                }
                            }
                        }

                        if (send_mqtt)
                        {
                            if (DataSimulation == 0)
                            {
                                if (ele_status == "IDL" || ele_status == "NOR")
                                {
                                    dt_lastreceive = DateTime.Now;
                                    run = 1;
                                }
                                else
                                {
                                    run = 0;
                                }
                                dataPoint.point = "yxzt";
                                dataPoint.value = run;
                                list.Add(dataPoint);
                                dataPoint = new MqttDeviceInfoDataPoint();
                                if (ele_status == "PKS")
                                {
                                    dt_lastreceive = DateTime.Now;
                                    pks = 1;
                                }
                                else
                                {
                                    pks = 0;
                                }
                                dataPoint.point = "stzt";
                                dataPoint.value = pks;
                                list.Add(dataPoint);
                                dataPoint = new MqttDeviceInfoDataPoint();
                                if (ele_status == "INS")
                                {
                                    dt_lastreceive = DateTime.Now;
                                    ins = 1;
                                }
                                else
                                {
                                    ins = 0;
                                }
                                dataPoint.point = "jxzt";
                                dataPoint.value = ins;
                                list.Add(dataPoint);
                                dataPoint = new MqttDeviceInfoDataPoint();
                                if (ele_status == "DTC" || ele_status == "NAV" || ele_status == "ESB" || ele_status == "UCM" || ele_status == "DTO" || ele_status == "ESB" || ele_status == "DBF" || ele_status == "DCS" || ele_status == "DLM" || ele_status == "HAD" || ele_status == "INI" || ele_status == "PRK")
                                {
                                    dt_lastreceive = DateTime.Now;
                                    falut = 1;
                                }
                                else
                                {
                                    falut = 0;
                                }
                                dataPoint.point = "gz";
                                dataPoint.value = falut;
                                list.Add(dataPoint);
                                dataPoint = new MqttDeviceInfoDataPoint();
                                heart = 0;

                                dataPoint.point = "txxt";
                                dataPoint.value = heart;
                                list.Add(dataPoint);
                                last_point = list;
                                List<MqttDeviceInfoData> infoDatas_list = new List<MqttDeviceInfoData>();
                                MqttDeviceInfoData infoData = new MqttDeviceInfoData();
                                infoData.deviceID = device_id;
                                infoData.devicePoints = list;
                                infoDatas_list.Add(infoData);
                                MqttDeviceInfo mqttDeviceInfo = new MqttDeviceInfo();
                                mqttDeviceInfo.msgid = Guid.NewGuid().ToString();
                                mqttDeviceInfo.ts = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
                                mqttDeviceInfo.data = infoDatas_list;
                                string message = JsonConvert.SerializeObject(mqttDeviceInfo);
                                if (mqttlog == 1)
                                {
                                    LogHelper.Log(message);
                                }
                                clientBUS.PublicMessage(topic, message, 0);
                                //LogHelper.Log("mqtt数据" + message);
                                mqtt_date = DateTime.Now;
                            }
                            else if (DataSimulation == 3)
                            {
                                //LogHelper.Log("电梯状态" + ele_status);
                                long ts = DateTimeOffset.Now.ToUnixTimeSeconds();
                                if (ele_status == "IDL" || ele_status == "NOR")
                                {
                                    dt_lastreceive = DateTime.Now;
                                    run = 1;
                                }
                                else
                                {
                                    run = 0;
                                }

                                if (ele_status == "PKS")
                                {
                                    dt_lastreceive = DateTime.Now;
                                    pks = 1;
                                }
                                else
                                {
                                    pks = 0;
                                }

                                if (ele_status == "INS")
                                {
                                    dt_lastreceive = DateTime.Now;
                                    ins = 1;
                                }
                                else
                                {
                                    ins = 0;
                                }

                                if (ele_status == "DTC" || ele_status == "NAV" || ele_status == "ESB" || ele_status == "UCM" || ele_status == "DTO" || ele_status == "ESB" || ele_status == "DBF" || ele_status == "DCS" || ele_status == "DLM" || ele_status == "HAD" || ele_status == "INI" || ele_status == "PRK")
                                {
                                    dt_lastreceive = DateTime.Now;
                                    falut = 1;
                                }
                                else
                                {
                                    falut = 0;
                                }


                                if (run == 1 || pks == 1 || ins == 1 || falut == 1)
                                {
                                    heart = 0;
                                }
                                else
                                {
                                    heart = 1;
                                }

                                if (heart == 0)
                                {
                                    CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                                    cZDT_YX_ZT.value = run == 1 ? true : false;
                                    cZDT_YX_ZT.ts = ts;
                                    dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                                    CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                                    cZDT_ST_ZT.value = pks == 1 ? true : false;
                                    cZDT_ST_ZT.ts = ts;
                                    dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                                    CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                                    cZDT_JX_ZT.value = ins == 1 ? true : false;
                                    cZDT_JX_ZT.ts = ts;
                                    dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                                    CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                                    cZDT_GZ_BJ.value = falut == 1 ? true : false;
                                    cZDT_GZ_BJ.ts = ts;
                                    dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                                    CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                                    cZDT_TX_BJ.value = heart == 0 ? true : false;
                                    cZDT_TX_BJ.ts = ts;
                                    dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;
                                    YYLDataPoint yYLDataPoint = new YYLDataPoint();
                                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                    byte[] bytes = new byte[4];
                                    rng.GetBytes(bytes);
                                    yYLDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();
                                    yYLDataPoint.ParamsInfo = dTParamInfo;

                                    string message = JsonConvert.SerializeObject(yYLDataPoint).Replace("ParamsInfo", "params");

                                    if (mqttlog == 1)
                                    {
                                        LogHelper.Log(message);
                                    }
                                    clientBUS.PublicMessage(topic, message, 0);
                                    //LogHelper.Log("mqtt数据" + message);
                                    mqtt_date = DateTime.Now;
                                }
                            }

                        }
                    }
                    //    if (byteSerialPortRecvS0_1[i] == 0xA1 && byteSerialPortRecvS0_1[i + 1] == 0x03 && byteSerialPortRecvS0_1[i + 39] == 0xD1)
                    //    {
                    //        dt_lastreceive = DateTime.Now;
                    //        byte[] buffer = new byte[40];
                    //        for (int j = 0; j < 40; j++)
                    //        {
                    //            buffer[j] = byteSerialPortRecvS0_1[i + j];
                    //        }
                    //        byte[] crcdata = new byte[37];
                    //        for (int k = 0; k < 37; k++)
                    //        {
                    //            crcdata[k] = buffer[k];
                    //        }
                    //        byte[] crc = Crc18(crcdata);
                    //        if ((crc[0] & 0x7F) == buffer[37] && (crc[1] & 0x7F) == buffer[38])
                    //        {
                    //            Array.Clear(byteSerialPortRecvS0_1, i, buffer.Length);
                    //            LogHelper.Log(ToHexs(buffer));
                    //            int run = 0;
                    //            int pks = 0;
                    //            int ins = 0;
                    //            int falut = 0;
                    //            int heart = 1;
                    //            byte[] data = new byte[3];
                    //            for (int k = 0; k < 3; k++)
                    //            {
                    //                data[k] = buffer[k + 7];
                    //            }
                    //            List<MqttDeviceInfoDataPoint> list = new List<MqttDeviceInfoDataPoint>();
                    //            MqttDeviceInfoDataPoint dataPoint = new MqttDeviceInfoDataPoint();
                    //            string ele_status = HexStringToASCII(data);
                    //            if (mqttlog == 1)
                    //            {
                    //                LogHelper.Log(ele_status);
                    //            }
                    //            string s1 = Convert.ToString(crcdata[34], 2).PadLeft(8, '0');
                    //            string s2 = Convert.ToString(crcdata[35], 2).PadLeft(8, '0');
                    //            bool send = false;
                    //            bool send_mqtt = false;
                    //            string data_str = ToHexs(buffer);

                    //            if (data_str != dt_point)
                    //            {
                    //                if (mqttsend == 1)
                    //                {
                    //                    send_mqtt = true;
                    //                }
                    //                send = true;
                    //                dt_point = data_str;
                    //            }
                    //            else
                    //            {
                    //                if (mqttsend == 1)
                    //                {
                    //                    TimeSpan mqtt_secondSpan = new TimeSpan(DateTime.Now.Ticks - mqtt_date.Ticks);
                    //                    if (mqtt_secondSpan.TotalMilliseconds > 1000)
                    //                    {
                    //                        send_mqtt = true;
                    //                    }
                    //                }
                    //                TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - dt_date.Ticks);
                    //                if (secondSpan.TotalMilliseconds > 1000)
                    //                {
                    //                    send = true;
                    //                }
                    //            }

                    //            if (send_mqtt)
                    //            {
                    //                if (DataSimulation == 0)
                    //                {
                    //                    if (ele_status == "IDL" || ele_status == "NOR")
                    //                    {
                    //                        run = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        run = 0;
                    //                    }
                    //                    dataPoint.point = "yxzt";
                    //                    dataPoint.value = run;
                    //                    list.Add(dataPoint);
                    //                    dataPoint = new MqttDeviceInfoDataPoint();
                    //                    if (ele_status == "PKS")
                    //                    {
                    //                        pks = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        pks = 0;
                    //                    }
                    //                    dataPoint.point = "stzt";
                    //                    dataPoint.value = pks;
                    //                    list.Add(dataPoint);
                    //                    dataPoint = new MqttDeviceInfoDataPoint();
                    //                    if (ele_status == "INS")
                    //                    {
                    //                        ins = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        ins = 0;
                    //                    }
                    //                    dataPoint.point = "jxzt";
                    //                    dataPoint.value = ins;
                    //                    list.Add(dataPoint);
                    //                    dataPoint = new MqttDeviceInfoDataPoint();
                    //                    if (ele_status == "DTC" || ele_status == "NAV" || ele_status == "ESB" || ele_status == "UCM" || ele_status == "DTO" || ele_status == "ESB" || ele_status == "DBF" || ele_status == "DCS" || ele_status == "DLM" || ele_status == "HAD" || ele_status == "INI" || ele_status == "PRK")
                    //                    {
                    //                        falut = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        falut = 0;
                    //                    }
                    //                    dataPoint.point = "gz";
                    //                    dataPoint.value = falut;
                    //                    list.Add(dataPoint);
                    //                    dataPoint = new MqttDeviceInfoDataPoint();
                    //                    if (s2.Substring(7, 1) == "1")
                    //                    {
                    //                        heart = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        heart = 0;
                    //                    }
                    //                    dataPoint.point = "txxt";
                    //                    dataPoint.value = heart;
                    //                    list.Add(dataPoint);
                    //                    last_point = list;
                    //                    List<MqttDeviceInfoData> infoDatas_list = new List<MqttDeviceInfoData>();
                    //                    MqttDeviceInfoData infoData = new MqttDeviceInfoData();
                    //                    infoData.deviceID = device_id;
                    //                    infoData.devicePoints = list;
                    //                    infoDatas_list.Add(infoData);
                    //                    MqttDeviceInfo mqttDeviceInfo = new MqttDeviceInfo();
                    //                    mqttDeviceInfo.msgid = Guid.NewGuid().ToString();
                    //                    mqttDeviceInfo.ts = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
                    //                    mqttDeviceInfo.data = infoDatas_list;
                    //                    string message = JsonConvert.SerializeObject(mqttDeviceInfo);
                    //                    if (mqttlog == 1)
                    //                    {
                    //                        LogHelper.Log(message);
                    //                    }
                    //                    clientBUS.PublicMessage(topic, message, 0);
                    //                    //LogHelper.Log("mqtt数据" + message);
                    //                    mqtt_date = DateTime.Now;
                    //                }
                    //                else if (DataSimulation == 3)
                    //                {
                    //                    //LogHelper.Log("电梯状态" + ele_status);
                    //                    long ts = DateTimeOffset.Now.ToUnixTimeSeconds();
                    //                    if (ele_status == "IDL" || ele_status == "NOR")
                    //                    {
                    //                        run = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        run = 0;
                    //                    }
                    //                    CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                    //                    cZDT_YX_ZT.value = run == 1 ? true : false;
                    //                    cZDT_YX_ZT.ts = ts;
                    //                    dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                    //                    if (ele_status == "PKS")
                    //                    {
                    //                        pks = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        pks = 0;
                    //                    }
                    //                    CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                    //                    cZDT_ST_ZT.value = pks == 1 ? true : false;
                    //                    cZDT_ST_ZT.ts = ts;
                    //                    dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                    //                    if (ele_status == "INS")
                    //                    {
                    //                        ins = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        ins = 0;
                    //                    }
                    //                    CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                    //                    cZDT_JX_ZT.value = ins == 1 ? true : false;
                    //                    cZDT_JX_ZT.ts = ts;
                    //                    dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                    //                    if (ele_status == "DTC" || ele_status == "NAV" || ele_status == "ESB" || ele_status == "UCM" || ele_status == "DTO" || ele_status == "ESB" || ele_status == "DBF" || ele_status == "DCS" || ele_status == "DLM" || ele_status == "HAD" || ele_status == "INI" || ele_status == "PRK")
                    //                    {
                    //                        falut = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        falut = 0;
                    //                    }
                    //                    CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                    //                    cZDT_GZ_BJ.value = falut == 1 ? true : false;
                    //                    cZDT_GZ_BJ.ts = ts;
                    //                    dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                    //                    if (s2.Substring(7, 1) == "1")
                    //                    {
                    //                        heart = 1;
                    //                    }
                    //                    else
                    //                    {
                    //                        heart = 0;
                    //                    }
                    //                    CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                    //                    cZDT_TX_BJ.value = heart == 0 ? true : false;
                    //                    cZDT_TX_BJ.ts = ts;
                    //                    dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;
                    //                    YYLDataPoint yYLDataPoint = new YYLDataPoint();
                    //                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                    //                    byte[] bytes = new byte[4];
                    //                    rng.GetBytes(bytes);
                    //                    yYLDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();
                    //                    yYLDataPoint.ParamsInfo = dTParamInfo;

                    //                    string message = JsonConvert.SerializeObject(yYLDataPoint).Replace("ParamsInfo", "params");

                    //                    if (mqttlog == 1)
                    //                    {
                    //                        LogHelper.Log(message);
                    //                    }
                    //                    clientBUS.PublicMessage(topic, message, 0);
                    //                    //LogHelper.Log("mqtt数据" + message);
                    //                    mqtt_date = DateTime.Now;
                    //                }

                    //            }

                    //            if (send)
                    //            {
                    //                int sx = 0;//上行
                    //                int xx = 0;//下行
                    //                int km = 0;//开门
                    //                int gm = 0;//关门
                    //                int xf = 0;//消防
                    //                int sj = 0;//司机
                    //                int st = 0;//锁梯
                    //                int zc = 0;//正常
                    //                int dl = 0;//独立
                    //                int jx = 0;//检修
                    //                int sxw = 0;//上限位
                    //                int xxw = 0;//下限位
                    //                int pc = 0;//平层
                    //                int cs = 0;//超速
                    //                int tcfw = 0;//退出服务
                    //                int gmdw = 0;//关门到位
                    //                int yr = 0;//有人
                    //                int xfyyx = 0;//消防员运行
                    //                int dd = 0;//断电
                    //                int dj = 0;//待机
                    //                int jl = 0;//警铃
                    //                int gz = 0;//故障
                    //                int by = 0;//保养
                    //                int wx = 0;//维修
                    //                int kr = 0;//困人
                    //                int ycbj = 0;//异常报警

                    //                if (pks == 1)
                    //                {
                    //                    st = 1;
                    //                }
                    //                byte[] bu = new byte[1];
                    //                bu[0] = buffer[3];
                    //                string yxzt = HexStringToASCII(bu);
                    //                if (yxzt == "u")
                    //                {
                    //                    sx = 1;
                    //                }
                    //                if (yxzt == "d")
                    //                {
                    //                    xx = 1;
                    //                }
                    //                byte[] door = new byte[2];
                    //                door[0] = buffer[14];
                    //                door[1] = buffer[15];
                    //                string door_statu = HexStringToASCII(door);
                    //                if (door_statu == "<>" || door_statu == "[]")
                    //                {
                    //                    km = 1;
                    //                }
                    //                if (door_statu == "<>" || door_statu == "][")
                    //                {
                    //                    gm = 1;
                    //                }
                    //                if (ele_status == "EFO")
                    //                {
                    //                    xf = 1;
                    //                }
                    //                if (run == 1)
                    //                {
                    //                    zc = 1;
                    //                }
                    //                if (ins == 1)
                    //                {
                    //                    jx = 1;
                    //                }
                    //                if (ele_status == "NAV")
                    //                {
                    //                    tcfw = 1;
                    //                }
                    //                if (door_statu == "][")
                    //                {
                    //                    gmdw = 1;
                    //                }
                    //                byte[] yr_statu = new byte[1];
                    //                yr_statu[0] = buffer[30];
                    //                string ss = HexStringToASCII(yr_statu);
                    //                if (HexStringToASCII(yr_statu) == "A")
                    //                {
                    //                    yr = 0;
                    //                }
                    //                else
                    //                {
                    //                    yr = 1;
                    //                }
                    //                if (ele_status == "EFS")
                    //                {
                    //                    xfyyx = 1;
                    //                }
                    //                if (s1.Substring(3, 1) == "1")
                    //                {
                    //                    dd = 1;
                    //                }
                    //                if (s1.Substring(5, 1) == "1")
                    //                {
                    //                    jl = 1;
                    //                }
                    //                if (ele_status == "IDL")
                    //                {
                    //                    dj = 1;
                    //                }
                    //                if (falut == 1)
                    //                {
                    //                    gz = 1;
                    //                }
                    //                if (s1.Substring(4, 1) == "1")
                    //                {
                    //                    wx = 1;
                    //                }

                    //                byte[] floor = new byte[2];
                    //                floor[0] = buffer[4];
                    //                floor[1] = buffer[5];
                    //                string floor_info = HexStringToASCII(floor);
                    //                string floor_info_str = Convert.ToString(floor[0]) + Convert.ToString(floor[1]);
                    //                int phy = 0;
                    //                for (int m = 0; m < show_list.Count; m++)
                    //                {
                    //                    if (show_list[m] == floor_info)
                    //                    {
                    //                        phy = phy_list[m];
                    //                    }
                    //                }

                    //                string d_6 = "0" + st.ToString() + sj.ToString() + xf.ToString() + gm.ToString() + km.ToString() + xx.ToString() + sx.ToString();
                    //                string d_7 = "0" + cs.ToString() + pc.ToString() + xxw.ToString() + sxw.ToString() + jx.ToString() + dl.ToString() + zc.ToString();
                    //                string d_8 = "0" + jl.ToString() + dj.ToString() + dd.ToString() + xfyyx.ToString() + yr.ToString() + gmdw.ToString() + tcfw.ToString();
                    //                string d_9 = "000" + ycbj.ToString() + kr.ToString() + wx.ToString() + by.ToString() + gz.ToString();
                    //                string d_10 = " 00";
                    //                string d_11 = " 00";
                    //                string d_12 = " 00";
                    //                string d_13 = " 00";
                    //                string d_14 = " 00";
                    //                string d_15 = " 00";
                    //                string d_16 = " 00";
                    //                string d_17 = " 00";
                    //                int dm = 1;
                    //                if (falut == 1)
                    //                {
                    //                    dm = 5;
                    //                    dt_status = false;
                    //                }
                    //                if (!dt_status && falut == 0)
                    //                {
                    //                    dm = 6;
                    //                    dt_status = true;
                    //                }
                    //                if (dm == 5 || dm == 6)
                    //                {
                    //                    d_12 = " " + DateTime.Now.ToString("yy");
                    //                    d_13 = " " + DateTime.Now.ToString("MM");
                    //                    d_14 = " " + DateTime.Now.ToString("dd");
                    //                    d_15 = " " + DateTime.Now.ToString("HH");
                    //                    d_16 = " " + DateTime.Now.ToString("mm");
                    //                    d_17 = " " + DateTime.Now.ToString("ss");
                    //                }
                    //                if (!string.IsNullOrEmpty(areaid))
                    //                {
                    //                    string cmd = "A1 03 48 " + liftid.ToString("X").PadLeft(2, '0') + " " + dm.ToString().PadLeft(2, '0') + " " + areaid.Substring(0, 2) + " " + areaid.Substring(2, 2) + " " + buffer[4].ToString("X").PadLeft(2, '0') + " " + buffer[5].ToString("X").PadLeft(2, '0') + " " + phy.ToString("X").PadLeft(2, '0') + " " + string.Format("{0:X}", Convert.ToInt32(d_6, 2)).PadLeft(2, '0') + " " + string.Format("{0:X}", Convert.ToInt32(d_7, 2)).PadLeft(2, '0') + " " + string.Format("{0:X}", Convert.ToInt32(d_8, 2)).PadLeft(2, '0') + " " + string.Format("{0:X}", Convert.ToInt32(d_9, 2)).PadLeft(2, '0') + d_10 + d_11 + d_12 + d_13 + d_14 + d_15 + d_16 + d_17 + " 00 00 00 00 00 D1";
                    //                    try
                    //                    {
                    //                        int result = server.Send(HexStringToByteArray(cmd));
                    //                        dt_date = DateTime.Now;
                    //                    }
                    //                    catch
                    //                    {
                    //                        ConnectServer(server_ip, server_port);
                    //                        string cmd_1 = string.Empty;
                    //                        string number_no = number.Substring(0, 2) + " " + number.Substring(2, 2) + " " + number.Substring(4, 2) + " " + number.Substring(6, 2) + " " + number.Substring(8, 2) + " " + number.Substring(10, 1);
                    //                        cmd_1 = "A1 01 16 00 00 " + number_no + "F D1";
                    //                        server.Send(HexStringToByteArray(cmd));
                    //                    }
                    //                }
                    //            }
                    //        }
                    //    }
                    //}
                }
            }
            catch (Exception ex)
            {

            }
            SerialPortRecvTimerS0_1.Start();
        }

        #region CRC校验
        public byte[] Crc18(byte[] buffer, int start = 0, int len = 0)
        {
            if (buffer == null || buffer.Length == 0) return null;
            if (start < 0) return null;
            if (len == 0) len = buffer.Length - start;
            int length = start + len;
            if (length > buffer.Length) return null;
            ushort crc = 0xFFFF;// Initial value
            for (int i = start; i < length; i++)
            {
                crc ^= buffer[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 1) > 0)
                        crc = (ushort)((crc >> 1) ^ 0xA001);// 0xA001 = reverse 0x8005
                    else
                        crc = (ushort)(crc >> 1);
                }
            }
            byte[] ret = BitConverter.GetBytes(crc);
            Array.Reverse(ret);
            return ret;
        }
        #endregion

        #region 16进制转ASCII
        public static string HexStringToASCII(byte[] bt)
        {
            string lin = "";
            for (int i = 0; i < bt.Length; i++)
            {
                lin = lin + bt[i] + " ";
            }


            string[] ss = lin.Trim().Split(new char[] { ' ' });
            char[] c = new char[ss.Length];
            int a;
            for (int i = 0; i < c.Length; i++)
            {
                a = Convert.ToInt32(ss[i]);
                c[i] = Convert.ToChar(a);
            }

            string b = new string(c);
            return b;
        }
        #endregion

        public static string ToHexStrFromByte(byte[] byteDatas)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < byteDatas.Length; i++)
            {
                builder.Append(string.Format("{0:X2} ", byteDatas[i]));
            }
            return builder.ToString().Trim();

        }
        #endregion

        #region ttys1 扶梯
        private Timer SerialPortRecvTimerS1_2;
        public event EventHandler<SerialPort485_2RecvEventArgs> ReceivedDataEventS1_2;
        public byte[] byteSerialPortRecvS1_2 = new byte[byteLength];
        private static int pSerialPortRecv_S1_2 = 0;

        public void OpenS1SerialPort(string portName, int baudRate, int parity, int dataBits, int stopBits)
        {
            try
            {
                SetS1SerialPortConfig_2(portName, baudRate, parity, dataBits, stopBits);
                myS1SerialPort_2.Open();
            }
            catch (Exception e)
            {
                logPrint.Log("ttyS1打开异常" + e.ToString());
            }
        }

        private void SetS1SerialPortConfig_2(string portName, int baudRate, int parity, int dataBits, int stopBits)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                portName = $"/dev/{portName}";
            }
            // 串口 参数设置
            myS1SerialPort_2.PortName = portName;
            myS1SerialPort_2.BaudRate = baudRate;
            switch (parity)
            {
                case 0:
                default:
                    myS1SerialPort_2.Parity = Parity.None;
                    break;
                case 1:
                    myS1SerialPort_2.Parity = Parity.Odd;
                    break;
                case 2:
                    myS1SerialPort_2.Parity = Parity.Even;
                    break;
                case 3:
                    myS1SerialPort_2.Parity = Parity.Mark;
                    break;
                case 4:
                    myS1SerialPort_2.Parity = Parity.Space;
                    break;
            }
            myS1SerialPort_2.DataBits = ((4 < dataBits) && (dataBits < 9)) ? dataBits : 8;
            switch (stopBits)
            {
                case 0:
                    myS1SerialPort_2.StopBits = StopBits.None;
                    break;
                case 1:
                default:
                    myS1SerialPort_2.StopBits = StopBits.One;
                    break;
                case 2:
                    myS1SerialPort_2.StopBits = StopBits.OnePointFive;
                    break;
                case 3:
                    myS1SerialPort_2.StopBits = StopBits.Two;
                    break;
            }
            myS1SerialPort_2.ReadTimeout = -1;
            myS1SerialPort_2.RtsEnable = true;
            myS1SerialPort_2.DataReceived += new SerialDataReceivedEventHandler(DataReceived_ttyS1);

            // 串口 接收数据处理定时器 参数设置
            SerialPortRecvTimerS1_2 = new Timer();
            SerialPortRecvTimerS1_2.Interval = 100;
            SerialPortRecvTimerS1_2.AutoReset = false;
            SerialPortRecvTimerS1_2.Elapsed += new ElapsedEventHandler(SPRecvTimer_Tick_ttyS1_2);
        }

        private void SPRecvTimer_Tick_ttyS1_2(object sender, EventArgs e)
        {
            byte[] TemporaryData = new byte[byteLength];
            int TemporaryDataLength = 0;

            if (ReceivedDataEventS1_2 != null)
            {
                byteSerialPortRecvS1_2.CopyTo(TemporaryData, 0);
                TemporaryDataLength = pSerialPortRecv_S1_2;

                ReceivedDataEventS1_2.Invoke(this, new SerialPort485_2RecvEventArgs(TemporaryData, TemporaryDataLength));
                // 数据处理完后，将指针指向数据头，等待接收新的数据
                pSerialPortRecv_S1_2 = 0;
            }
        }

        public class SerialPort485_2RecvEventArgs : EventArgs
        {
            /// <summary>
            /// 广播 收到的串口数据
            /// </summary>
            public readonly byte[] RecvData_485_2 = new byte[byteLength];

            /// <summary>
            /// 收到数据 的 长度
            /// </summary>
            public readonly int RecvDataLength_485_2;

            /// <summary>
            /// 将 收到的数据 转化成 待广播的数据
            /// </summary>
            public SerialPort485_2RecvEventArgs(byte[] recvData, int recvDataLength)
            {
                recvData.CopyTo(RecvData_485_2, 0);
                RecvDataLength_485_2 = recvDataLength;
            }
        }

        private void DataReceived_ttyS1(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialPortRecvTimerS1_2.Stop();

                byte[] ReadBuf = new byte[myS1SerialPort_2.BytesToRead];
                if (pSerialPortRecv_S1_2 > byteLength - ReadBuf.Length)
                {
                    pSerialPortRecv_S1_2 = 0;
                }
                myS1SerialPort_2.Read(ReadBuf, 0, ReadBuf.Length);
                ReadBuf.CopyTo(byteSerialPortRecvS1_2, pSerialPortRecv_S1_2);
                pSerialPortRecv_S1_2 += ReadBuf.Length;
                if (mqttlog == 1)
                {
                    LogHelper.Log("扶梯_receive:" + ToHexs(ReadBuf));
                }
                try
                {
                    for (int i = 0; i < byteSerialPortRecvS1_2.Length; i++)
                    {
                        if (byteSerialPortRecvS1_2[i] == liftid && byteSerialPortRecvS1_2[i + 1] == 0x03)
                        {
                            string strCmd = string.Empty;
                            int numcount = Convert.ToInt32(byteSerialPortRecvS1_2[i + 2]);
                            byte[] buffers = new byte[numcount + 3];
                            for (int j = 0; j < buffers.Length; j++)
                            {
                                buffers[j] = byteSerialPortRecvS1_2[i + j];
                            }
                            byte[] crc = Crc18(buffers);
                            if (crc[0] == byteSerialPortRecvS1_2[i + numcount + 4] && crc[1] == byteSerialPortRecvS1_2[i + numcount + 3])
                            {
                                Array.Clear(byteSerialPortRecvS1_2, i, numcount + 5);
                                isconnect = true;
                                lastreceive = DateTime.Now;
                                string data_1 = Convert.ToString(buffers[4], 2).PadLeft(8, '0');
                                string data_2 = Convert.ToString(buffers[6], 2).PadLeft(8, '0');
                                byte[] buffer_data = new byte[2];
                                buffer_data[0] = buffers[7];
                                buffer_data[1] = buffers[8];
                                string falut_code = ToHexs(buffer_data);
                                int code = Convert.ToInt32(falut_code, 16);
                                List<MqttDeviceInfoDataPoint> list = new List<MqttDeviceInfoDataPoint>();
                                MqttDeviceInfoDataPoint dataPoint = new MqttDeviceInfoDataPoint();
                                int dirction = 0;
                                int dover_Plate_Alarm = 0;
                                int emergency_Stop = 0;
                                int fault = 0;
                                int healtbeat = 1;

                                bool send = false;
                                bool send_mqtt = false;
                                string data_str = ToHexs(buffers);

                                if (data_str != ft_point)
                                {
                                    if (mqttsend == 1)
                                    {
                                        send_mqtt = true;
                                    }
                                    send = true;
                                    ft_point = data_str;
                                }
                                else
                                {
                                    if (mqttsend == 1)
                                    {
                                        TimeSpan mqtt_secondSpan = new TimeSpan(DateTime.Now.Ticks - mqtt_date.Ticks);
                                        if (mqtt_secondSpan.TotalMilliseconds > 1000)
                                        {
                                            send_mqtt = true;
                                        }
                                    }
                                    TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - ft_date.Ticks);
                                    if (secondSpan.TotalMilliseconds > 1000)
                                    {
                                        send = true;
                                    }
                                }

                                if (send_mqtt)
                                {
                                    if (DataSimulation == 0)
                                    {
                                        if (data_1.Substring(4, 1) == "1")
                                        {
                                            dirction = 2;
                                        }
                                        if (data_1.Substring(2, 1) == "1")
                                        {
                                            dirction = 1;
                                        }
                                        if (data_1.Substring(5, 1) == "1")
                                        {
                                            dirction = 0;
                                        }
                                        dataPoint.point = "yxfx";
                                        dataPoint.value = dirction;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();

                                        if (buffers[7] == 0xFF && buffers[8] == 0xFF)
                                        {
                                            fault = 0;
                                        }
                                        else
                                        {
                                            fault = 1;
                                        }
                                        dataPoint.point = "gz";
                                        dataPoint.value = fault;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();



                                        if (code == 60 || code == 59)
                                        {
                                            emergency_Stop = 1;
                                        }
                                        dataPoint.point = "jtbj";
                                        dataPoint.value = emergency_Stop;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();

                                        if (code == 2 || code == 3)
                                        {
                                            dover_Plate_Alarm = 1;
                                        }
                                        dataPoint.point = "gbfdbj";
                                        dataPoint.value = dover_Plate_Alarm;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();

                                        dataPoint.point = "txxt";
                                        dataPoint.value = healtbeat;
                                        list.Add(dataPoint);
                                        last_point = list;
                                        List<MqttDeviceInfoData> infoDatas_list = new List<MqttDeviceInfoData>();
                                        MqttDeviceInfoData infoData = new MqttDeviceInfoData();
                                        infoData.deviceID = device_id;
                                        infoData.devicePoints = list;
                                        infoDatas_list.Add(infoData);
                                        MqttDeviceInfo mqttDeviceInfo = new MqttDeviceInfo();
                                        mqttDeviceInfo.msgid = Guid.NewGuid().ToString();
                                        mqttDeviceInfo.ts = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
                                        mqttDeviceInfo.data = infoDatas_list;
                                        string message = JsonConvert.SerializeObject(mqttDeviceInfo);
                                        if (mqttlog == 1)
                                        {
                                            LogHelper.Log(message);
                                        }
                                        clientBUS.PublicMessage(topic, message, 0);
                                        mqtt_date = DateTime.Now;
                                    }
                                    else if (DataSimulation == 3)
                                    {
                                        long ts = DateTimeOffset.Now.ToUnixTimeSeconds();
                                        ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                        if (data_1.Substring(4, 1) == "1")//上行
                                        {

                                            zDFT_SX_ZT.value = true;
                                            dirction = 1;
                                        }
                                        else
                                        {
                                            zDFT_SX_ZT.value = false;
                                        }
                                        zDFT_SX_ZT.ts = ts;
                                        fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                        ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                        if (data_1.Substring(2, 1) == "1")//下行
                                        {
                                            zDFT_XX_ZT.value = true;
                                            dirction = 2;
                                        }
                                        else
                                        {
                                            zDFT_XX_ZT.value = false;
                                        }
                                        zDFT_XX_ZT.ts = ts;
                                        fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                        ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                        if (data_1.Substring(5, 1) == "1")//停止
                                        {
                                            zDFT_TZ_ZT.value = true;
                                            dirction = 0;
                                        }
                                        else
                                        {
                                            zDFT_TZ_ZT.value = false;
                                        }
                                        zDFT_TZ_ZT.ts = ts;
                                        fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;

                                        ZDFT_GZ_BJ zDFT_GZ_BJ = new ZDFT_GZ_BJ();
                                        if (buffers[7] == 0xFF && buffers[8] == 0xFF)
                                        {
                                            fault = 0;
                                            zDFT_GZ_BJ.value = false;
                                        }
                                        else
                                        {
                                            fault = 1;
                                            zDFT_GZ_BJ.value = true;
                                        }
                                        zDFT_GZ_BJ.ts = ts;
                                        fTParamInfo.ZDFT_GZ_BJ = zDFT_GZ_BJ;

                                        ZDFT_JT_BJ zDFT_JT_BJ = new ZDFT_JT_BJ();
                                        if (code == 60 || code == 59)
                                        {
                                            emergency_Stop = 1;
                                            zDFT_JT_BJ.value = true;
                                        }
                                        else
                                        {
                                            zDFT_JT_BJ.value = false;
                                        }
                                        zDFT_JT_BJ.ts = ts;
                                        fTParamInfo.ZDFT_JT_BJ = zDFT_JT_BJ;

                                        ZDFT_GB_BJ zDFT_GB_BJ = new ZDFT_GB_BJ();
                                        if (code == 2 || code == 3)
                                        {
                                            dover_Plate_Alarm = 1;
                                            zDFT_GB_BJ.value = true;
                                        }
                                        else
                                        {
                                            zDFT_GB_BJ.value = false;
                                        }
                                        zDFT_GB_BJ.ts = ts;
                                        fTParamInfo.ZDFT_GB_BJ = zDFT_GB_BJ;
                                        ZDFT_TX_BJ zDFT_TX_BJ = new ZDFT_TX_BJ();
                                        zDFT_TX_BJ.value = true;
                                        zDFT_TX_BJ.ts = ts;
                                        fTParamInfo.ZDFT_TX_BJ = zDFT_TX_BJ;

                                        YYLDataPoint yYLDataPoint = new YYLDataPoint();
                                        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                        byte[] bytes = new byte[4];
                                        rng.GetBytes(bytes);
                                        yYLDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();
                                        yYLDataPoint.ParamsInfo = fTParamInfo;

                                        string message = JsonConvert.SerializeObject(yYLDataPoint).Replace("ParamsInfo", "params");
                                        if (mqttlog == 1)
                                        {
                                            LogHelper.Log(message);
                                        }
                                        clientBUS.PublicMessage(topic, message, 0);
                                        mqtt_date = DateTime.Now;
                                    }

                                }

                                if (send)
                                {
                                    int sx = 0;//上行
                                    int xx = 0;//下行
                                    int km = 0;//开门
                                    int gm = 0;//关门
                                    int xf = 0;//消防
                                    int sj = 0;//司机
                                    int st = 0;//锁梯
                                    int zc = 0;//正常
                                    int dl = 0;//独立
                                    int jx = 0;//检修
                                    int sxw = 0;//上限位
                                    int xxw = 0;//下限位
                                    int pc = 0;//平层
                                    int cs = 0;//超速
                                    int tcfw = 0;//退出服务
                                    int gmdw = 0;//关门到位
                                    int yr = 0;//有人
                                    int xfyyx = 0;//消防员运行
                                    int dd = 0;//断电
                                    int dj = 0;//待机
                                    int jl = 0;//警铃
                                    int gz = 0;//故障
                                    int by = 0;//保养
                                    int wx = 0;//维修
                                    int kr = 0;//困人
                                    int ycbj = 0;//异常报警

                                    if (dirction == 1)
                                    {
                                        sx = 1;
                                    }
                                    if (dirction == 2)
                                    {
                                        xx = 1;
                                    }
                                    if (fault == 1)
                                    {
                                        gz = 1;
                                    }
                                    if (data_1.Substring(3, 1) == "1")
                                    {
                                        jx = 1;
                                    }

                                    string d_6 = "0" + st.ToString() + sj.ToString() + xf.ToString() + gm.ToString() + km.ToString() + xx.ToString() + sx.ToString();
                                    string d_7 = "0" + cs.ToString() + pc.ToString() + xxw.ToString() + sxw.ToString() + jx.ToString() + dl.ToString() + zc.ToString();
                                    string d_8 = "0" + jl.ToString() + dj.ToString() + dd.ToString() + xfyyx.ToString() + yr.ToString() + gmdw.ToString() + tcfw.ToString();
                                    string d_9 = "000" + ycbj.ToString() + kr.ToString() + wx.ToString() + by.ToString() + gz.ToString();
                                    string d_10 = " 00";
                                    string d_11 = " 00";
                                    string d_12 = " 00";
                                    string d_13 = " 00";
                                    string d_14 = " 00";
                                    string d_15 = " 00";
                                    string d_16 = " 00";
                                    string d_17 = " 00";
                                    int dm = 1;
                                    if (fault == 1)
                                    {
                                        dm = 5;
                                        ft_status = false;
                                        d_10 = " " + code.ToString().PadLeft(4, '0').Substring(0, 2);
                                        d_11 = " " + code.ToString().PadLeft(4, '0').Substring(2, 2);
                                    }
                                    if (!ft_status && fault == 0)
                                    {
                                        dm = 6;
                                        ft_status = true;
                                    }
                                    if (dm == 5 || dm == 6)
                                    {
                                        d_12 = " " + DateTime.Now.ToString("yy");
                                        d_13 = " " + DateTime.Now.ToString("MM");
                                        d_14 = " " + DateTime.Now.ToString("dd");
                                        d_15 = " " + DateTime.Now.ToString("HH");
                                        d_16 = " " + DateTime.Now.ToString("mm");
                                        d_17 = " " + DateTime.Now.ToString("ss");
                                    }

                                    if (!string.IsNullOrEmpty(areaid))
                                    {
                                        string cmd = "A1 03 48 " + liftid.ToString("X").PadLeft(2, '0') + " " + dm.ToString().PadLeft(2, '0') + " " + areaid.Substring(0, 2) + " " + areaid.Substring(2, 2) + " 00 00 00 " + string.Format("{0:X}", Convert.ToInt32(d_6, 2)).PadLeft(2, '0') + " " + string.Format("{0:X}", Convert.ToInt32(d_7, 2)).PadLeft(2, '0') + " " + string.Format("{0:X}", Convert.ToInt32(d_8, 2)).PadLeft(2, '0') + " " + string.Format("{0:X}", Convert.ToInt32(d_9, 2)).PadLeft(2, '0') + d_10 + d_11 + d_12 + d_13 + d_14 + d_15 + d_16 + d_17 + " 00 00 00 00 00 D1";
                                        try
                                        {
                                            server.Send(HexStringToByteArray(cmd));
                                            ft_date = DateTime.Now;
                                        }
                                        catch
                                        {
                                            ConnectServer(server_ip, server_port);
                                            string cmd_1 = string.Empty;
                                            string number_no = number.Substring(0, 2) + " " + number.Substring(2, 2) + " " + number.Substring(4, 2) + " " + number.Substring(6, 2) + " " + number.Substring(8, 2) + " " + number.Substring(10, 1);
                                            cmd_1 = "A1 01 16 00 00 " + number_no + "F D1";
                                            server.Send(HexStringToByteArray(cmd));
                                        }
                                    }

                                }
                            }
                        }

                    }
                }
                catch (Exception ex)
                {
                }
                SerialPortRecvTimerS1_2.Start();
            }
            catch (Exception)
            {
            }
        }

        public string ToHexs(byte[] data)
        {
            if (data.Length <= 0) return "";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.Append(data[i].ToString("X2") + " ");
            }
            string str = sb.ToString();
            return str;
        }

        #region 查询扶梯状态
        public void QueryElevatorStatus()
        {
            try
            {
                Task task = new Task(async () =>
                {
                    if (cmd_token.IsCancellationRequested)
                    {
                        return;
                    }
                    cmd_resetEvent.WaitOne();
                    //do something
                    DateTime startTime = DateTime.Now;
                    byte[] cmd_buffer = new byte[6];
                    cmd_buffer[0] = (byte)(liftid & 0xff);
                    cmd_buffer[1] = 0x03;
                    cmd_buffer[2] = 0x00;
                    cmd_buffer[3] = 0x00;
                    cmd_buffer[4] = 0x00;
                    cmd_buffer[5] = 0x05;
                    byte[] crc = Crc18(cmd_buffer);
                    byte[] data_crc = new byte[2];
                    data_crc[0] = crc[1];
                    data_crc[1] = crc[0];
                    string cmd = ToHexStrFromByte(cmd_buffer) + " " + ToHexStrFromByte(data_crc);
                    while (true)
                    {
                        try
                        {
                            TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - startTime.Ticks);
                            if (secondSpan.TotalMilliseconds > 500)
                            {
                                QueryttyS1_Status(cmd);
                                startTime = DateTime.Now;
                                TimeSpan heartSpan = new TimeSpan(DateTime.Now.Ticks - lastreceive.Ticks);
                                if (heartSpan.TotalMilliseconds > 1000 /*&& isconnect*/)
                                {
                                    if (DataSimulation == 0)
                                    {
                                        List<MqttDeviceInfoData> infoDatas_list = new List<MqttDeviceInfoData>();
                                        MqttDeviceInfoData infoData = new MqttDeviceInfoData();
                                        infoData.deviceID = device_id;
                                        if (last_point == null)
                                        {
                                            List<MqttDeviceInfoDataPoint> dataPoints = new List<MqttDeviceInfoDataPoint>();
                                            MqttDeviceInfoDataPoint dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "txxt";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);

                                            dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "yxfx";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);

                                            dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "gz";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);

                                            dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "jtbj";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);

                                            dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "gbfdbj";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);
                                            infoData.devicePoints = dataPoints;
                                        }
                                        else
                                        {
                                            for (int i = 0; i < last_point.Count; i++)
                                            {
                                                if (last_point[i].point == "txxt")
                                                {
                                                    last_point.Remove(last_point[i]);
                                                }
                                            }
                                            MqttDeviceInfoDataPoint dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "txxt";
                                            dataPoint.value = 0;
                                            last_point.Add(dataPoint);
                                            infoData.devicePoints = last_point;
                                            infoDatas_list.Add(infoData);
                                        }

                                        MqttDeviceInfo mqttDeviceInfo = new MqttDeviceInfo();
                                        mqttDeviceInfo.msgid = Guid.NewGuid().ToString();
                                        mqttDeviceInfo.ts = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
                                        mqttDeviceInfo.data = infoDatas_list;
                                        string message = JsonConvert.SerializeObject(mqttDeviceInfo);
                                        if (mqttlog == 1)
                                        {
                                            LogHelper.Log(message);
                                        }
                                        clientBUS.PublicMessage(topic, message, 0);
                                        isconnect = false;
                                        lastreceive = DateTime.Now;
                                    }
                                    else if (DataSimulation == 3)
                                    {
                                        long ts = DateTimeOffset.Now.ToUnixTimeSeconds();
                                        YYLDataPoint yYLDataPoint = new YYLDataPoint();
                                        if (fTParamInfo.ZDFT_TX_BJ == null)
                                        {
                                            ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                            zDFT_SX_ZT.value = false;
                                            zDFT_SX_ZT.ts = ts;
                                            fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                            ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                            zDFT_XX_ZT.value = false;
                                            zDFT_XX_ZT.ts = ts;
                                            fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                            ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                            zDFT_TZ_ZT.value = false;
                                            zDFT_TZ_ZT.ts = ts;
                                            fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;

                                            ZDFT_GZ_BJ zDFT_GZ_BJ = new ZDFT_GZ_BJ();
                                            zDFT_GZ_BJ.value = false;
                                            zDFT_GZ_BJ.ts = ts;
                                            fTParamInfo.ZDFT_GZ_BJ = zDFT_GZ_BJ;

                                            ZDFT_JT_BJ zDFT_JT_BJ = new ZDFT_JT_BJ();
                                            zDFT_JT_BJ.value = false;
                                            zDFT_JT_BJ.ts = ts;
                                            fTParamInfo.ZDFT_JT_BJ = zDFT_JT_BJ;

                                            ZDFT_GB_BJ zDFT_GB_BJ = new ZDFT_GB_BJ();
                                            zDFT_GB_BJ.value = false;
                                            zDFT_GB_BJ.ts = ts;
                                            fTParamInfo.ZDFT_GB_BJ = zDFT_GB_BJ;

                                            ZDFT_TX_BJ zDFT_TX_BJ = new ZDFT_TX_BJ();
                                            zDFT_TX_BJ.value = false;
                                            zDFT_TX_BJ.ts = ts;
                                            fTParamInfo.ZDFT_TX_BJ = zDFT_TX_BJ;
                                        }
                                        else
                                        {
                                            fTParamInfo.ZDFT_TX_BJ.value = false;
                                            fTParamInfo.ZDFT_TX_BJ.ts = ts;
                                            fTParamInfo.ZDFT_GB_BJ.ts = ts;
                                            fTParamInfo.ZDFT_JT_BJ.ts = ts;
                                            fTParamInfo.ZDFT_GZ_BJ.ts = ts;
                                            fTParamInfo.ZDFT_TZ_ZT.ts = ts;
                                            fTParamInfo.ZDFT_XX_ZT.ts = ts;
                                            fTParamInfo.ZDFT_SX_ZT.ts = ts;
                                        }

                                        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                        byte[] bytes = new byte[4];
                                        rng.GetBytes(bytes);
                                        yYLDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();
                                        yYLDataPoint.ParamsInfo = fTParamInfo;

                                        string message = JsonConvert.SerializeObject(yYLDataPoint).Replace("ParamsInfo", "params");
                                        if (mqttlog == 1)
                                        {
                                            LogHelper.Log(message);
                                        }
                                        clientBUS.PublicMessage(topic, message, 0);
                                        isconnect = false;
                                        lastreceive = DateTime.Now;
                                    }

                                }
                            }
                        }
                        catch
                        {

                        }
                    }
                }, cmd_token);
                task.Start();
            }
            catch (Exception e)
            {
            }
        }

        public void QueryttyS1_Status(string cmd)
        {
            try
            {
                ttyS1SendMsg(cmd);
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.ToString());
                return;
            }
        }

        public bool ttyS1SendMsg(string data)
        {
            try
            {
                myS1SerialPort_2.Write(ToBytesSendDate(data), 0, ToBytesSendDate(data).Length);
                return true;
            }
            catch (Exception e)
            {
                //LogHelper.Log("命令发送失败" + data + e.ToString());
                return false;
            }
        }

        public byte[] ToBytesSendDate(string str)
        {
            List<byte> result = new List<byte>();
            if (str.Contains(" "))
            {
                string[] strs = str.Trim().Split(' ');
                foreach (string s in strs)
                {
                    result.Add(Convert.ToByte(s.Trim(), 16));
                }
            }
            else
            {
                result.Add(Convert.ToByte(str, 16));
            }
            return result.ToArray();
        }
        #endregion

        #region 查询电梯状态
        public void QueryDTElevatorStatus()
        {
            try
            {
                Task task = new Task(async () =>
                {
                    if (dt_token.IsCancellationRequested)
                    {
                        return;
                    }
                    dt_resetEvent.WaitOne();
                    //do something
                    DateTime startTime = DateTime.Now;
                    while (true)
                    {
                        try
                        {
                            TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - startTime.Ticks);
                            if (secondSpan.TotalMilliseconds > 1000)
                            {
                                startTime = DateTime.Now;
                                TimeSpan heartSpan = new TimeSpan(DateTime.Now.Ticks - dt_lastreceive.Ticks);
                                if (heartSpan.TotalMilliseconds > 1000)
                                {
                                    if (DataSimulation == 0)
                                    {
                                        List<MqttDeviceInfoData> infoDatas_list = new List<MqttDeviceInfoData>();
                                        MqttDeviceInfoData infoData = new MqttDeviceInfoData();
                                        infoData.deviceID = device_id;
                                        if (last_point == null)
                                        {
                                            List<MqttDeviceInfoDataPoint> dataPoints = new List<MqttDeviceInfoDataPoint>();
                                            MqttDeviceInfoDataPoint dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "txxt";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);

                                            dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "gz";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);

                                            dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "jxzt";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);

                                            dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "stzt";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);

                                            dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "yxzt";
                                            dataPoint.value = 0;
                                            dataPoints.Add(dataPoint);
                                            infoData.devicePoints = dataPoints;
                                        }
                                        else
                                        {
                                            for (int i = 0; i < last_point.Count; i++)
                                            {
                                                if (last_point[i].point == "txxt")
                                                {
                                                    last_point.Remove(last_point[i]);
                                                }
                                            }
                                            MqttDeviceInfoDataPoint dataPoint = new MqttDeviceInfoDataPoint();
                                            dataPoint.point = "txxt";
                                            dataPoint.value = 0;
                                            last_point.Add(dataPoint);
                                            infoData.devicePoints = last_point;
                                            infoDatas_list.Add(infoData);
                                        }

                                        MqttDeviceInfo mqttDeviceInfo = new MqttDeviceInfo();
                                        mqttDeviceInfo.msgid = Guid.NewGuid().ToString();
                                        mqttDeviceInfo.ts = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
                                        mqttDeviceInfo.data = infoDatas_list;
                                        string message = JsonConvert.SerializeObject(mqttDeviceInfo);
                                        if (mqttlog == 1)
                                        {
                                            LogHelper.Log(message);
                                        }
                                        clientBUS.PublicMessage(topic, message, 0);
                                        isconnect = false;
                                        dt_lastreceive = DateTime.Now;
                                    }
                                    else if (DataSimulation == 3)
                                    {
                                        long ts = DateTimeOffset.Now.ToUnixTimeSeconds();
                                        YYLDataPoint yYLDataPoint = new YYLDataPoint();

                                        if (heartSpan.TotalMilliseconds < off_time)
                                        {
                                            if (dTParamInfo.CZDT_TX_BJ == null)
                                            {
                                                CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                                                cZDT_YX_ZT.value = false;
                                                cZDT_YX_ZT.ts = ts;
                                                dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                                                CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                                                cZDT_ST_ZT.value = false;
                                                cZDT_ST_ZT.ts = ts;
                                                dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                                                CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                                                cZDT_JX_ZT.value = false;
                                                cZDT_JX_ZT.ts = ts;
                                                dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                                                CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                                                cZDT_GZ_BJ.value = false;
                                                cZDT_GZ_BJ.ts = ts;
                                                dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                                                CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                                                cZDT_TX_BJ.value = false;
                                                cZDT_TX_BJ.ts = ts;
                                                dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;

                                            }
                                            else
                                            {
                                                dTParamInfo.CZDT_TX_BJ.ts = ts;
                                                dTParamInfo.CZDT_GZ_BJ.ts = ts;
                                                dTParamInfo.CZDT_JX_ZT.ts = ts;
                                                dTParamInfo.CZDT_ST_ZT.ts = ts;
                                                dTParamInfo.CZDT_YX_ZT.ts = ts;
                                            }
                                        }
                                        else
                                        {
                                            if (dTParamInfo.CZDT_TX_BJ == null)
                                            {
                                                CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                                                cZDT_YX_ZT.value = false;
                                                cZDT_YX_ZT.ts = ts;
                                                dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                                                CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                                                cZDT_ST_ZT.value = false;
                                                cZDT_ST_ZT.ts = ts;
                                                dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                                                CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                                                cZDT_JX_ZT.value = false;
                                                cZDT_JX_ZT.ts = ts;
                                                dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                                                CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                                                cZDT_GZ_BJ.value = false;
                                                cZDT_GZ_BJ.ts = ts;
                                                dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                                                CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                                                cZDT_TX_BJ.value = false;
                                                cZDT_TX_BJ.ts = ts;
                                                dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;

                                            }
                                            else
                                            {
                                                if (dTParamInfo.CZDT_TX_BJ.value)
                                                {
                                                    LogHelper.Log("超过时间未收到正确数据");
                                                    dTParamInfo.CZDT_TX_BJ.value = false;
                                                    dTParamInfo.CZDT_GZ_BJ.value = false;
                                                    dTParamInfo.CZDT_JX_ZT.value = false;
                                                    dTParamInfo.CZDT_ST_ZT.value = false;
                                                    dTParamInfo.CZDT_YX_ZT.value = false;
                                                }
                                                dTParamInfo.CZDT_TX_BJ.ts = ts;
                                                dTParamInfo.CZDT_GZ_BJ.ts = ts;
                                                dTParamInfo.CZDT_JX_ZT.ts = ts;
                                                dTParamInfo.CZDT_ST_ZT.ts = ts;
                                                dTParamInfo.CZDT_YX_ZT.ts = ts;
                                            }
                                        }

                                        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                        byte[] bytes = new byte[4];
                                        rng.GetBytes(bytes);
                                        yYLDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();
                                        yYLDataPoint.ParamsInfo = dTParamInfo;

                                        string message = JsonConvert.SerializeObject(yYLDataPoint).Replace("ParamsInfo", "params");
                                        if (mqttlog == 1)
                                        {
                                            LogHelper.Log(message);
                                        }
                                        clientBUS.PublicMessage(topic, message, 0);
                                        isconnect = false;
                                        //dt_lastreceive = DateTime.Now;
                                    }
                                }
                            }
                        }
                        catch
                        {

                        }
                    }
                }, dt_token);
                task.Start();
            }
            catch (Exception e)
            {
            }
        }
        #endregion

        #endregion
        #region 连接服务平台
        public void Connect(string ip, int port)
        {
            Task task = new Task(async () =>
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
                resetEvent.WaitOne();
                //do something
                ConnectToPlatform(ip, port);

            }, token);
            task.Start();
        }
        public void ConnectToPlatform(string serverip, int port)
        {
            bool result = false;
            while (!result)
            {
                result = ConnectServer(serverip, port);
            }
            string cmd = string.Empty;
            string number_no = number.Substring(0, 2) + " " + number.Substring(2, 2) + " " + number.Substring(4, 2) + " " + number.Substring(6, 2) + " " + number.Substring(8, 2) + " " + number.Substring(10, 1);
            cmd = "A1 01 16 00 00 " + number_no + "F D1";
            server.Send(HexStringToByteArray(cmd));
        }

        public bool ConnectServer(string serverIP, int port)
        {
            try
            {
                server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Connect(serverIP, port);
                thread = new Thread(StartReceive);
                thread.Start(server);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public void StartReceive(object obj)
        {
            while (true)
            {
                try
                {
                    byte[] buffer = new byte[2048];

                    int lenght = server.Receive(buffer);
                    for (int i = 0; i < lenght; i++)
                    {
                        if (buffer[i] == 0xA1 && buffer[i + 1] == 0x01 && buffer[i + 21] == 0xD1)
                        {
                            areaid = Convert.ToString(buffer[i + 5], 16).PadLeft(2, '0') + Convert.ToString(buffer[i + 6], 16).PadLeft(2, '0');

                        }
                        else if (buffer[i] == 0xA1 && buffer[i + 1] == 0x02 && buffer[i + 21] == 0xD1)
                        {
                            string date = "20" + Convert.ToString(buffer[i + 2], 16).PadLeft(2, '0') + "-" + Convert.ToString(buffer[i + 3], 16).PadLeft(2, '0') + "-" + Convert.ToString(buffer[i + 4], 16).PadLeft(2, '0') + " " + Convert.ToString(buffer[i + 5], 16).PadLeft(2, '0') + ":" + Convert.ToString(buffer[i + 6], 16).PadLeft(2, '0') + ":" + Convert.ToString(buffer[i + 7], 16).PadLeft(2, '0');
                            string command = "date -s '" + date + "'";
                            LogHelper.Log("设置时间" + command);
                            Process.Start("sudo", command);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Log(ex.ToString());
                }

            }
        }

        public bool SendMsg(string str)
        {

            try
            {
                string senddata = string.Format(@"{0}", str);
                bool result = false;
                while (!result)
                {
                    if (server.Connected == true)
                    {

                        try
                        {
                            server.Send(HexStringToByteArray(senddata));
                        }
                        catch
                        {
                            return false;
                        }
                        return true;

                    }
                    else
                    {

                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static byte[] HexStringToByteArray(string s)
        {
            s = s.Replace(" ", "");
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
            return buffer;
        }
        #endregion


        public static CancellationTokenSource dataSimulation_tokenSource = new CancellationTokenSource();
        public CancellationToken dataSimulation_token = dataSimulation_tokenSource.Token;
        public ManualResetEvent dataSimulation_resetEvent = new ManualResetEvent(true);
        public static CancellationTokenSource YYL_dataSimulation_tokenSource = new CancellationTokenSource();
        public CancellationToken YYL_dataSimulation_token = YYL_dataSimulation_tokenSource.Token;
        public ManualResetEvent YYL_dataSimulation_resetEvent = new ManualResetEvent(true);
        public void dataSimulation()
        {
            try
            {
                Task task = new Task(async () =>
                {
                    if (dataSimulation_token.IsCancellationRequested)
                    {
                        return;
                    }
                    dataSimulation_resetEvent.WaitOne();

                    //do something
                    while (true)
                    {
                        try
                        {
                            if (dataSimulation_point.Count > 0)
                            {
                                foreach (var item in dataSimulation_point)
                                {
                                    int send = 0;
                                    int ele_type = 0;//1=电梯，2=扶梯
                                    List<MqttDeviceInfoDataPoint> list = new List<MqttDeviceInfoDataPoint>();
                                    MqttDeviceInfoDataPoint dataPoint = new MqttDeviceInfoDataPoint();
                                    MqttDeviceInfoDataPoint dataPoint_2 = new MqttDeviceInfoDataPoint();
                                    if (item.point == "dt_run" && item.value != 0)
                                    {
                                        ele_type = 1;
                                        send = 1;
                                        dataPoint.point = "yxzt";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "stzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jxzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "dt_pks" && item.value != 0)
                                    {
                                        ele_type = 1;
                                        send = 1;
                                        dataPoint.point = "yxzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "stzt";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jxzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "dt_ins" && item.value != 0)
                                    {
                                        ele_type = 1;
                                        send = 1;
                                        dataPoint.point = "yxzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "stzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jxzt";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "dt_fault" && item.value != 0)
                                    {
                                        ele_type = 1;
                                        send = 1;
                                        dataPoint.point = "yxzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "stzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jxzt";
                                        dataPoint.value = 0;

                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "ft_fault" && item.value != 0)
                                    {
                                        ele_type = 2;
                                        send = 1;
                                        dataPoint.point = "yxfx";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gbfdbj";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jtbj";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "dt_healtbeat" && item.value != 0)
                                    {
                                        send = 1;
                                        ele_type = 2;
                                        dataPoint.point = "yxzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "stzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jxzt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);

                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "ft_healtbeat" && item.value != 0)
                                    {
                                        send = 1;
                                        ele_type = 2;
                                        dataPoint.point = "yxfx";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gbfdbj";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jtbj";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "ft_dirction" && item.value != 0)
                                    {
                                        send = 1;
                                        ele_type = 2;
                                        dataPoint.point = "yxfx";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gbfdbj";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jtbj";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "ft_dover_Plate_Alarm" && item.value != 0)
                                    {
                                        send = 1;
                                        ele_type = 2;
                                        dataPoint.point = "yxfx";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gbfdbj";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jtbj";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                    }
                                    else if (item.point == "ft_emergency_Stop" && item.value != 0)
                                    {
                                        send = 1;
                                        ele_type = 2;
                                        dataPoint.point = "yxfx";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gbfdbj";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "jtbj";
                                        dataPoint.value = item.value;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "gz";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                        dataPoint = new MqttDeviceInfoDataPoint();
                                        dataPoint.point = "txxt";
                                        dataPoint.value = 0;
                                        list.Add(dataPoint);
                                    }
                                    if (send == 1)
                                    {
                                        List<MqttDeviceInfoData> infoDatas_list = new List<MqttDeviceInfoData>();
                                        MqttDeviceInfoData infoData = new MqttDeviceInfoData();
                                        if (ele_type == 1)
                                        {
                                            infoData.deviceID = "11111";
                                        }
                                        if (ele_type == 2)
                                        {
                                            infoData.deviceID = device_id;
                                        }

                                        infoData.devicePoints = list;
                                        infoDatas_list.Add(infoData);
                                        MqttDeviceInfo mqttDeviceInfo = new MqttDeviceInfo();
                                        mqttDeviceInfo.msgid = Guid.NewGuid().ToString();
                                        mqttDeviceInfo.ts = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
                                        mqttDeviceInfo.data = infoDatas_list;
                                        string message = JsonConvert.SerializeObject(mqttDeviceInfo);
                                        if (ele_type == 1)
                                        {
                                            if (mqttlog == 1)
                                            {
                                                LogHelper.Log(message);
                                            }
                                            clientBUS.PublicMessage("v1/17/hqhcz/DDATA/0/11111", message, 0);
                                        }
                                        if (ele_type == 2)
                                        {
                                            if (mqttlog == 1)
                                            {
                                                LogHelper.Log(message);
                                            }
                                            clientBUS.PublicMessage(topic, message, 0);
                                        }

                                        Wait(VirtualInterval);
                                    }
                                }
                                //startTime = DateTime.Now;
                                //}
                            }
                        }
                        catch (Exception ex)
                        {

                        }

                    }

                }, dataSimulation_token);
                task.Start();
            }
            catch (Exception e)
            {
            }
        }

        public void YYLdataSimulation()
        {
            try
            {
                Task task = new Task(async () =>
                {
                    if (YYL_dataSimulation_token.IsCancellationRequested)
                    {
                        return;
                    }
                    YYL_dataSimulation_resetEvent.WaitOne();

                    //do something
                    while (true)
                    {
                        foreach (var item in dataSimulation_point)
                        {
                            int send = 0;
                            int ele_type = 0;//1=电梯，2=扶梯
                            List<YYLMqttDeviceInfoDataPoint> list = new List<YYLMqttDeviceInfoDataPoint>();
                            YYLMqttDeviceInfoDataPoint dataPoint = new YYLMqttDeviceInfoDataPoint();
                            YYLMqttDeviceInfoDataPoint dataPoint_2 = new YYLMqttDeviceInfoDataPoint();
                            long ts = DateTimeOffset.Now.ToUnixTimeSeconds();

                            DTParamInfo dTParamInfo = new DTParamInfo();
                            FTParamInfo fTParamInfo = new FTParamInfo();

                            if (item.point == "dt_run" && item.value != 0)
                            {
                                ele_type = 1;
                                send = 1;
                                CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                                cZDT_YX_ZT.ts = ts;
                                cZDT_YX_ZT.value = item.value == 1 ? true : false;
                                dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                                CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                                cZDT_ST_ZT.ts = ts;
                                cZDT_ST_ZT.value = false;
                                dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                                CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                                cZDT_JX_ZT.ts = ts;
                                cZDT_JX_ZT.value = false;
                                dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                                CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                                cZDT_GZ_BJ.ts = ts;
                                cZDT_GZ_BJ.value = false;
                                dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                                CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                                cZDT_TX_BJ.ts = ts;
                                cZDT_TX_BJ.value = false;
                                dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;
                            }
                            else if (item.point == "dt_pks" && item.value != 0)
                            {
                                ele_type = 1;
                                send = 1;
                                CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                                cZDT_YX_ZT.ts = ts;
                                cZDT_YX_ZT.value = false;
                                dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                                CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                                cZDT_ST_ZT.ts = ts;
                                cZDT_ST_ZT.value = item.value == 1 ? true : false;
                                dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                                CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                                cZDT_JX_ZT.ts = ts;
                                cZDT_JX_ZT.value = false;
                                dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                                CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                                cZDT_GZ_BJ.ts = ts;
                                cZDT_GZ_BJ.value = false;
                                dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                                CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                                cZDT_TX_BJ.ts = ts;
                                cZDT_TX_BJ.value = false;
                                dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;
                            }
                            else if (item.point == "dt_ins" && item.value != 0)
                            {
                                ele_type = 1;
                                send = 1;
                                CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                                cZDT_YX_ZT.ts = ts;
                                cZDT_YX_ZT.value = false;
                                dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                                CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                                cZDT_ST_ZT.ts = ts;
                                cZDT_ST_ZT.value = false;
                                dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                                CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                                cZDT_JX_ZT.ts = ts;
                                cZDT_JX_ZT.value = item.value == 1 ? true : false;
                                dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                                CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                                cZDT_GZ_BJ.ts = ts;
                                cZDT_GZ_BJ.value = false;
                                dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                                CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                                cZDT_TX_BJ.ts = ts;
                                cZDT_TX_BJ.value = false;
                                dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;
                            }
                            else if (item.point == "dt_fault" && item.value != 0)
                            {
                                ele_type = 1;
                                send = 1;
                                CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                                cZDT_YX_ZT.ts = ts;
                                cZDT_YX_ZT.value = false;
                                dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                                CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                                cZDT_ST_ZT.ts = ts;
                                cZDT_ST_ZT.value = false;
                                dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                                CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                                cZDT_JX_ZT.ts = ts;
                                cZDT_JX_ZT.value = false;
                                dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                                CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                                cZDT_GZ_BJ.ts = ts;
                                cZDT_GZ_BJ.value = item.value == 1 ? true : false;
                                dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                                CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                                cZDT_TX_BJ.ts = ts;
                                cZDT_TX_BJ.value = false;
                                dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;
                            }
                            else if (item.point == "dt_healtbeat" && item.value != 0)
                            {
                                send = 1;
                                ele_type = 1;
                                CZDT_YX_ZT cZDT_YX_ZT = new CZDT_YX_ZT();
                                cZDT_YX_ZT.ts = ts;
                                cZDT_YX_ZT.value = false;
                                dTParamInfo.CZDT_YX_ZT = cZDT_YX_ZT;

                                CZDT_ST_ZT cZDT_ST_ZT = new CZDT_ST_ZT();
                                cZDT_ST_ZT.ts = ts;
                                cZDT_ST_ZT.value = false;
                                dTParamInfo.CZDT_ST_ZT = cZDT_ST_ZT;

                                CZDT_JX_ZT cZDT_JX_ZT = new CZDT_JX_ZT();
                                cZDT_JX_ZT.ts = ts;
                                cZDT_JX_ZT.value = false;
                                dTParamInfo.CZDT_JX_ZT = cZDT_JX_ZT;

                                CZDT_GZ_BJ cZDT_GZ_BJ = new CZDT_GZ_BJ();
                                cZDT_GZ_BJ.ts = ts;
                                cZDT_GZ_BJ.value = false;
                                dTParamInfo.CZDT_GZ_BJ = cZDT_GZ_BJ;

                                CZDT_TX_BJ cZDT_TX_BJ = new CZDT_TX_BJ();
                                cZDT_TX_BJ.ts = ts;
                                cZDT_TX_BJ.value = item.value == 1 ? true : false;
                                dTParamInfo.CZDT_TX_BJ = cZDT_TX_BJ;
                            }
                            else if (item.point == "ft_fault" && item.value != 0)
                            {
                                ele_type = 2;
                                send = 1;
                                ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                zDFT_SX_ZT.ts = ts;
                                zDFT_SX_ZT.value = false;
                                fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                zDFT_XX_ZT.ts = ts;
                                zDFT_XX_ZT.value = false;
                                fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                zDFT_TZ_ZT.ts = ts;
                                zDFT_TZ_ZT.value = false;
                                fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;

                                ZDFT_GB_BJ zDFT_GB_BJ = new ZDFT_GB_BJ();
                                zDFT_GB_BJ.ts = ts;
                                zDFT_GB_BJ.value = false;
                                fTParamInfo.ZDFT_GB_BJ = zDFT_GB_BJ;

                                ZDFT_JT_BJ zDFT_JT_BJ = new ZDFT_JT_BJ();
                                zDFT_JT_BJ.ts = ts;
                                zDFT_JT_BJ.value = false;
                                fTParamInfo.ZDFT_JT_BJ = zDFT_JT_BJ;

                                ZDFT_GZ_BJ zDFT_GZ_BJ = new ZDFT_GZ_BJ();
                                zDFT_GZ_BJ.ts = ts;
                                zDFT_GZ_BJ.value = item.value == 1 ? true : false;
                                fTParamInfo.ZDFT_GZ_BJ = zDFT_GZ_BJ;

                                ZDFT_TX_BJ zDFT_TX_BJ = new ZDFT_TX_BJ();
                                zDFT_TX_BJ.ts = ts;
                                zDFT_TX_BJ.value = false;
                                fTParamInfo.ZDFT_TX_BJ = zDFT_TX_BJ;
                            }
                            else if (item.point == "ft_healtbeat" && item.value != 0)
                            {
                                send = 1;
                                ele_type = 2;
                                ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                zDFT_SX_ZT.ts = ts;
                                zDFT_SX_ZT.value = false;
                                fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                zDFT_XX_ZT.ts = ts;
                                zDFT_XX_ZT.value = false;
                                fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                zDFT_TZ_ZT.ts = ts;
                                zDFT_TZ_ZT.value = false;
                                fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;

                                ZDFT_GB_BJ zDFT_GB_BJ = new ZDFT_GB_BJ();
                                zDFT_GB_BJ.ts = ts;
                                zDFT_GB_BJ.value = false;
                                fTParamInfo.ZDFT_GB_BJ = zDFT_GB_BJ;

                                ZDFT_JT_BJ zDFT_JT_BJ = new ZDFT_JT_BJ();
                                zDFT_JT_BJ.ts = ts;
                                zDFT_JT_BJ.value = false;
                                fTParamInfo.ZDFT_JT_BJ = zDFT_JT_BJ;

                                ZDFT_GZ_BJ zDFT_GZ_BJ = new ZDFT_GZ_BJ();
                                zDFT_GZ_BJ.ts = ts;
                                zDFT_GZ_BJ.value = false;
                                fTParamInfo.ZDFT_GZ_BJ = zDFT_GZ_BJ;

                                ZDFT_TX_BJ zDFT_TX_BJ = new ZDFT_TX_BJ();
                                zDFT_TX_BJ.ts = ts;
                                zDFT_TX_BJ.value = item.value == 1 ? true : false;
                                fTParamInfo.ZDFT_TX_BJ = zDFT_TX_BJ;
                            }
                            else if (item.point == "ft_dirction")
                            {
                                send = 1;
                                ele_type = 2;
                                if (item.value == 1)
                                {
                                    ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                    zDFT_SX_ZT.ts = ts;
                                    zDFT_SX_ZT.value = true;
                                    fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                    ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                    zDFT_XX_ZT.ts = ts;
                                    zDFT_XX_ZT.value = false;
                                    fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                    ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                    zDFT_TZ_ZT.ts = ts;
                                    zDFT_TZ_ZT.value = false;
                                    fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;
                                }
                                else if (item.value == 2)
                                {
                                    ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                    zDFT_SX_ZT.ts = ts;
                                    zDFT_SX_ZT.value = false;
                                    fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                    ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                    zDFT_XX_ZT.ts = ts;
                                    zDFT_XX_ZT.value = true;
                                    fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                    ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                    zDFT_TZ_ZT.ts = ts;
                                    zDFT_TZ_ZT.value = false;
                                    fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;
                                }
                                else
                                {
                                    ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                    zDFT_SX_ZT.ts = ts;
                                    zDFT_SX_ZT.value = false;
                                    fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                    ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                    zDFT_XX_ZT.ts = ts;
                                    zDFT_XX_ZT.value = false;
                                    fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                    ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                    zDFT_TZ_ZT.ts = ts;
                                    zDFT_TZ_ZT.value = true;
                                    fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;
                                }
                                ZDFT_GB_BJ zDFT_GB_BJ = new ZDFT_GB_BJ();
                                zDFT_GB_BJ.ts = ts;
                                zDFT_GB_BJ.value = false;
                                fTParamInfo.ZDFT_GB_BJ = zDFT_GB_BJ;

                                ZDFT_JT_BJ zDFT_JT_BJ = new ZDFT_JT_BJ();
                                zDFT_JT_BJ.ts = ts;
                                zDFT_JT_BJ.value = false;
                                fTParamInfo.ZDFT_JT_BJ = zDFT_JT_BJ;

                                ZDFT_GZ_BJ zDFT_GZ_BJ = new ZDFT_GZ_BJ();
                                zDFT_GZ_BJ.ts = ts;
                                zDFT_GZ_BJ.value = false;
                                fTParamInfo.ZDFT_GZ_BJ = zDFT_GZ_BJ;

                                ZDFT_TX_BJ zDFT_TX_BJ = new ZDFT_TX_BJ();
                                zDFT_TX_BJ.ts = ts;
                                zDFT_TX_BJ.value = false;
                                fTParamInfo.ZDFT_TX_BJ = zDFT_TX_BJ;
                            }
                            else if (item.point == "ft_dover_Plate_Alarm" && item.value != 0)
                            {
                                send = 1;
                                ele_type = 2;
                                ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                zDFT_SX_ZT.ts = ts;
                                zDFT_SX_ZT.value = false;
                                fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                zDFT_XX_ZT.ts = ts;
                                zDFT_XX_ZT.value = false;
                                fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                zDFT_TZ_ZT.ts = ts;
                                zDFT_TZ_ZT.value = false;
                                fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;

                                ZDFT_GB_BJ zDFT_GB_BJ = new ZDFT_GB_BJ();
                                zDFT_GB_BJ.ts = ts;
                                zDFT_GB_BJ.value = item.value == 1 ? true : false;
                                fTParamInfo.ZDFT_GB_BJ = zDFT_GB_BJ;

                                ZDFT_JT_BJ zDFT_JT_BJ = new ZDFT_JT_BJ();
                                zDFT_JT_BJ.ts = ts;
                                zDFT_JT_BJ.value = false;
                                fTParamInfo.ZDFT_JT_BJ = zDFT_JT_BJ;

                                ZDFT_GZ_BJ zDFT_GZ_BJ = new ZDFT_GZ_BJ();
                                zDFT_GZ_BJ.ts = ts;
                                zDFT_GZ_BJ.value = false;
                                fTParamInfo.ZDFT_GZ_BJ = zDFT_GZ_BJ;

                                ZDFT_TX_BJ zDFT_TX_BJ = new ZDFT_TX_BJ();
                                zDFT_TX_BJ.ts = ts;
                                zDFT_TX_BJ.value = false;
                                fTParamInfo.ZDFT_TX_BJ = zDFT_TX_BJ;
                            }
                            else if (item.point == "ft_emergency_Stop" && item.value != 0)
                            {
                                send = 1;
                                ele_type = 2;
                                ZDFT_SX_ZT zDFT_SX_ZT = new ZDFT_SX_ZT();
                                zDFT_SX_ZT.ts = ts;
                                zDFT_SX_ZT.value = false;
                                fTParamInfo.ZDFT_SX_ZT = zDFT_SX_ZT;

                                ZDFT_XX_ZT zDFT_XX_ZT = new ZDFT_XX_ZT();
                                zDFT_XX_ZT.ts = ts;
                                zDFT_XX_ZT.value = false;
                                fTParamInfo.ZDFT_XX_ZT = zDFT_XX_ZT;

                                ZDFT_TZ_ZT zDFT_TZ_ZT = new ZDFT_TZ_ZT();
                                zDFT_TZ_ZT.ts = ts;
                                zDFT_TZ_ZT.value = false;
                                fTParamInfo.ZDFT_TZ_ZT = zDFT_TZ_ZT;

                                ZDFT_GB_BJ zDFT_GB_BJ = new ZDFT_GB_BJ();
                                zDFT_GB_BJ.ts = ts;
                                zDFT_GB_BJ.value = false;
                                fTParamInfo.ZDFT_GB_BJ = zDFT_GB_BJ;

                                ZDFT_JT_BJ zDFT_JT_BJ = new ZDFT_JT_BJ();
                                zDFT_JT_BJ.ts = ts;
                                zDFT_JT_BJ.value = item.value == 1 ? true : false;
                                fTParamInfo.ZDFT_JT_BJ = zDFT_JT_BJ;

                                ZDFT_GZ_BJ zDFT_GZ_BJ = new ZDFT_GZ_BJ();
                                zDFT_GZ_BJ.ts = ts;
                                zDFT_GZ_BJ.value = false;
                                fTParamInfo.ZDFT_GZ_BJ = zDFT_GZ_BJ;

                                ZDFT_TX_BJ zDFT_TX_BJ = new ZDFT_TX_BJ();
                                zDFT_TX_BJ.ts = ts;
                                zDFT_TX_BJ.value = false;
                                fTParamInfo.ZDFT_TX_BJ = zDFT_TX_BJ;
                            }
                            if (send == 1)
                            {
                                YYLDataPoint yYLDataPoint = new YYLDataPoint();
                                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                                byte[] bytes = new byte[4];
                                rng.GetBytes(bytes);
                                yYLDataPoint.msgid = BitConverter.ToUInt32(bytes, 0).ToString();
                                if (ele_type == 1)
                                {
                                    yYLDataPoint.ParamsInfo = dTParamInfo;
                                }
                                if (ele_type == 2)
                                {
                                    yYLDataPoint.ParamsInfo = fTParamInfo;
                                }

                                string message = JsonConvert.SerializeObject(yYLDataPoint).Replace("ParamsInfo", "params");
                                if (ele_type == 1)
                                {
                                    if (mqttlog == 1)
                                    {
                                        LogHelper.Log(message);
                                    }
                                    clientBUS.PublicMessage(topic_2, message, 0);
                                }
                                if (ele_type == 2)
                                {
                                    if (mqttlog == 1)
                                    {
                                        LogHelper.Log(message);
                                    }
                                    clientBUS.PublicMessage(topic, message, 0);
                                }

                                Wait(VirtualInterval);
                            }
                        }
                        //startTime = DateTime.Now;
                        //}
                    }

                }, YYL_dataSimulation_token);
                task.Start();
            }
            catch (Exception e)
            {
            }
        }


        public bool Wait(int second)
        {
            try
            {
                DateTime startTime = DateTime.Now;
                while (true)
                {
                    TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - startTime.Ticks);
                    if (secondSpan.TotalMilliseconds > second)
                    {
                        break;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
